package com.client.action;

import com.admin.bean.FindInfor;
import com.client.service.FindLookService;
import com.data.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;

/**
 * 发现查看
 *
 * @author:HingLo
 * @create 2017-10-20 9:34
 **/
@RequestMapping("/findLook/client/")
@Controller
public class FindLookAction {

    @Autowired
    private FindLookService findLookService;


    /**
     * 进入到发现页，返回发现页的数据信息，
     *
     * @param request
     * @return
     */
    @RequestMapping("findList")
    @ResponseBody
    public Map<String, Object> FindListAction(HttpServletRequest request) {
        List<?> list = findLookService.FindListService();
        String[] value = {"id", "title", "tag", "type", "url"};
        String url = JsonUtils.getBasePath(request) + "/findLook/client/indexLookImage?id=";
        return SoftwareMassageUtils.getSoftwareMassageUtils(list, request, url, value);
    }


    /**
     * 发现页的图片查看
     * * @param id
     *
     * @param response
     */

    @RequestMapping("indexLookImage")
    public void lookContentImageAction(String id, HttpServletResponse response) {
        if (CheckDataUtils.stringUtilsID(id)) {
            id = id.trim();
            FindInfor findInfor = findLookService.lookContentImageService(id);
            if (findInfor != null) {
                byte[] inputStream = findInfor.getImage();
                if (inputStream != null) {
                    ImageLookUtils.responseImage(inputStream, response);
                }

            }
        }
    }


    /***
     * 查看发现的具体内容
     * @param id
     * @param request
     * @return
     */
    @RequestMapping("lookfindContents")
    public String lookfindContentsAction(String id, HttpServletRequest request) {
        //更新阅读次数
        if (CheckDataUtils.stringUtilsID(id)) {
            id = id.trim();
            FindInfor findInfor = findLookService.lookContentImageService(id);
            request.setAttribute("dataMap", findInfor);

            return "/WEB-INF/client/postMsg";
        }
        return "404";
    }


    /**
     * 访问资讯中的图片
     *
     * @param id
     */
    @GetMapping("lookImage")
    public void lookImageAction(String id, HttpServletResponse response) {
        //获取文件的后缀
        String s = id.substring(id.lastIndexOf("."));
        if (CheckDataUtils.iconUtils(s)) {
            byte[] inputSteam = findLookService.lookImageService(id);
            ImageLookUtils.responseImage(inputSteam, response);
        } else if (CheckDataUtils.videoUtils(s)) {
            video(id, response);
        }


    }


    /**
     * 视频响应
     *
     * @param id
     * @param response
     * @throws Exception
     */
    public void video(String id, HttpServletResponse response) {
        String path = FinalStringUtils.FINDPATH + "image/" + id;
        File file = new File(path);
        FileInputStream in = null;
        try {
            in = new FileInputStream(file);
            ServletOutputStream out = response.getOutputStream();
            byte[] b = null;
            while (in.available() > 0) {
                if (in.available() > 10240) {
                    b = new byte[10240];
                } else {
                    b = new byte[in.available()];
                }
                in.read(b, 0, b.length);
                out.write(b, 0, b.length);
            }
            in.close();
            out.flush();
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
